/*****************************************************************************
*Purpose: Balance for Gautreaux children's outcomes analysis  
******************************************************************************/ 



** Predict earnings and neighborhood racial comp in control group
** Earnings
reg earn24 $controls i.yrbirth if estsample==1 & generalarea==0
predict prearn24
replace prearn24=. if earn24==.

** Predict future neighborhood Share Black in control group
reg m2019_pctblack $clist if estsample==1 & generalarea==0
predict prm2019_pctblack
replace prm2019_pctblack=. if m2019_pctblack==.

** Set estimation sample
global clist "female head_female headage married auto license work emp inc bed ph chicago ageatintake"
reghdfe generalarea $clist if origsamp==1 & earn24!=. , absorb( mitrct80 intakeyear) cluster(hhid)
gen this_samp=e(sample)

********************************
******** Balance Test ********** 
********************************


capture frame drop balance
frame create balance str35 outcome str35 group str35 spec beta se piks treatpiks controlpiks dv1treatpiks dv1controlpiks dv0treatpiks dv0controlpiks

foreach xj in $clist prearn24 prm2019_pctblack {
	reghdfe `xj' generalarea if this_samp==1, absorb( mitrct80 intakeyear ) cluster(hhid)
	unique pik if e(sample)==1

	if "`xj'"=="female" | "`xj'"=="married" | "`xj'"=="auto" | "`xj'"=="license" | "`xj'"=="work"  | "`xj'"=="ph" | "`xj'"=="chicago" | "`xj'"=="head_female" {    
	local allpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==1 & `xj'==1  
	local y1treatpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==0 & `xj'==1
	local y1controlpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==1 & `xj'==0
	local y0treatpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==0 & `xj'==0
	local y0controlpik `=r(sum)'
	frame post balance ("`xj'") ("all") ("diff")  (_b[generalarea]) (_se[generalarea]) (`allpik') (-99999) (-99999) (`y1treatpik') (`y1controlpik') (`y0treatpik') (`y0controlpik')
	}
	
	if "`xj'"=="headage" | "`xj'"=="inc" | "`xj'"=="emp" | "`xj'"=="ageatintake" | "`xj'"=="bed"| "`xj'"=="prearn24" | "`xj'"=="prm2019_pctblack"   {    
	local allpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==1  
	local treatpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==0  
	local controlpik `=r(sum)'

	frame post balance ("`xj'") ("all") ("diff")  (_b[generalarea]) (_se[generalarea]) (`allpik') (`treatpik') (`controlpik') (-99999) (-99999) (-99999) (-99999)
	}
	
	
	
	
	sum `xj' if e(sample)==1 & generalarea==0
	frame post balance ("`xj'") ("all") ("meansd")   (r(mean)) (r(sd)) (r(N)) (-99999) (-99999) (-99999) (-99999) (-99999) (-99999)
		
}

*********************************
****** Joint Orthoginality ****** 
*********************************

global clist "female head_female headage married auto license work emp inc bed ph chicago ageatintake"
reghdfe generalarea $clist if this_samp==1 & earn24!=. , absorb( mitrct80 intakeyear ) cluster(hhid)
global pv: di %6.4fc `=Ftail(e(df_m), e(df_r), e(F))'
di $pv
di e(F)
frame post balance ("fstat") ("all")  ("Fstat_pval") (e(F)) ($pv) (`allpik') (-99999) (-99999) (-99999) (-99999) (-99999) (-99999)  

*************************************************************
**** Balance - Continuous Placement Tract Characteristic ****
*************************************************************

frame change default

**** For Appendix Table I

foreach out in prearn24 prm2019_pctblack {
	foreach treat in std_place_shwhite std_place_shafam std_place_povrate {

	reghdfe `out' `treat' if this_samp==1, absorb( mitrct80 intakeyear ) cluster(hhid)
	unique pik if e(sample)==1
	local allpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==1  
	local treatpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==0  
	local controlpik `=r(sum)'
	
	frame post balance ("`out'") ("all") ("`treat'")  (_b[`treat']) (_se[`treat']) (`allpik') (-99999) (-99999) (`y1treatpik') (`y1controlpik') (`y0treatpik') (`y0controlpik')

	}
}


frame balance: save "${output}/balance_kids_012024.dta", replace
frame balance: export delimited using "${output}/balance_kids_012024.csv", replace